package com.atguigu.service;

import com.atguigu.dao.EmployeeMapper;
import com.atguigu.pojo.Employee;
import com.atguigu.pojo.EmployeeExample;
import com.atguigu.pojo.Msg;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class EmployeeService {
    @Autowired
    EmployeeMapper employeeMapper;


    /**
     * 查询所有员工信息
     * @return
     */
    public List<Employee> getAll() {

        return employeeMapper.selectByExampleWithDept(new EmployeeExample());
    }

    /**
     * 员工保存方法
     * @param employee
     */
    public Msg saveEmp(Employee employee) {
        employeeMapper.insertSelective(employee);
        return Msg.success();
    }

    /**
     * 检验用户名是否可用
     * @param empName
     * @return  true：代表当前姓名可用，false：当前姓名则不可用
     */
    public boolean checkUser(String empName) {
        EmployeeExample employeeExample = new EmployeeExample();
        EmployeeExample.Criteria criteria = employeeExample.createCriteria();
        criteria.andEmpNameEqualTo(empName);
        long count = employeeMapper.countByExample(employeeExample);
        return count == 0;
    }

    /**
     * 按照员工id查询员工
     * @param id
     * @return
     */
    public Employee getEmp(Integer id) {
        return employeeMapper.selectByPrimaryKey(id);
    }

    /**
     * 员工更新
     * @param employee
     */
    public void updateEmp(Employee employee) {
        employeeMapper.updateByPrimaryKeySelective(employee);
    }

    /**
     * 员工删除
     * @param id
     */
    public void deleteEmp(Integer id) {
        employeeMapper.deleteByPrimaryKey(id);
    }

    public void deleteBatch(List<Integer> ids) {
        EmployeeExample employeeExample = new EmployeeExample();
        EmployeeExample.Criteria criteria = employeeExample.createCriteria();
        criteria.andEmpIdIn(ids);
        employeeMapper.deleteByExample(employeeExample);
    }

    public List<Employee> selectByName(String searchValue) {
        System.out.println("selectByname" + searchValue);
        return employeeMapper.selectByName(searchValue);
    }

    public List<Employee> selectById(String searchValue) {
        System.out.println("selectByID");
        return employeeMapper.selectById(searchValue);
    }
}
